<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
      <el-breadcrumb-item :to="{ path: '/sys-product/temp/product/add-new' }">
        <i class="el-icon-s-promotion"></i> 商品管理
      </el-breadcrumb-item>
      <el-breadcrumb-item>新增商品</el-breadcrumb-item>
      <el-breadcrumb-item>第2步：商品详情</el-breadcrumb-item>
    </el-breadcrumb>

    <el-divider></el-divider>

    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="基本信息">
        <el-descriptions :title="this.ruleForm.name">
          <el-descriptions-item label="商品名称" span="1">
            <el-tag size="small">{{ this.ruleForm.name }}</el-tag>
          </el-descriptions-item>
          <el-descriptions-item label="类别" span="2">{{ this.ruleForm.categoryName }}</el-descriptions-item>
          <el-descriptions-item label="供应商" span="3">{{ this.ruleForm.mfrs }}</el-descriptions-item>
          <el-descriptions-item label="包装" span="3">{{ this.ruleForm.packing }}</el-descriptions-item>
          <el-descriptions-item label="型号" span="3">{{ this.ruleForm.model }}</el-descriptions-item>
          <el-descriptions-item label="规格">{{ this.ruleForm.standard }}</el-descriptions-item>
          <el-descriptions-item label="颜色">{{ this.ruleForm.color }}</el-descriptions-item>
          <el-descriptions-item label="零售价">{{ this.ruleForm.retailPrice }}</el-descriptions-item>
          <el-descriptions-item label="最低售价">{{ this.ruleForm.lowPrice }}</el-descriptions-item>
          <el-descriptions-item label="库存">{{ this.ruleForm.inventory }}</el-descriptions-item>
          <el-descriptions-item label="计量单位">{{ this.ruleForm.unit }}</el-descriptions-item>
          <el-descriptions-item label="是否启用">{{ this.ruleForm.enableName }}</el-descriptions-item>
          <el-descriptions-item label="备注">{{ this.ruleForm.remark }}</el-descriptions-item>
        </el-descriptions>
      </el-form-item>
      <el-form-item>
        <el-button type="warning" @click="backToPreStep()">上一步</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')">下一步</el-button>
      </el-form-item>
    </el-form>


  </div>
</template>

<script>
export default {
  data() {
    return {
      ruleForm: {
        enableName: '',
      },
      rules: {}
    };
  },
  methods: {
    backToPreStep() {
      this.$router.push('add-new1');
    },
    loadLocalRuleForm() {
      let localRuleFormString = localStorage.getItem('ruleForm');
      if (localRuleFormString) {
        let localRuleForm = JSON.parse(localRuleFormString);
        this.ruleForm = localRuleForm;
        if (localRuleForm.enable == 1) {
          this.ruleForm.enableName = '是';
        } else {
          this.ruleForm.enableName = '否';
        }
      }
    },
    submitForm(formName) {
      let url = 'http://localhost:8080/products/add-new';
      console.log('url = ' + url);
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .post(url, this.ruleForm).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.$message.success('添加成功');
              localStorage.removeItem('ruleForm');
              this.$router.push('add-new1');

        } else {
          this.$message.error(responseBody.message);
        }
      })
    }
  },
  mounted() {
    this.loadLocalRuleForm();
  }
}
</script>